﻿IF OBJECT_ID('up_MapHtml_GpsVisitDistributionPoleSelectClosest') IS not NULL
DROP PROC up_MapHtml_GpsVisitDistributionPoleSelectClosest

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'up_MapHtml_GpsVisitDistributionPoleSelectClosest')
	DROP PROCEDURE up_MapHtml_InspVisitDistributionPoleSelectClosest
GO

CREATE PROCEDURE [dbo].[up_MapHtml_GpsVisitDistributionPoleSelectClosest] 
(
	@latitude	float,
	@longitude	float,
	@key		int
)
AS

DECLARE @IdList varchar(MAX)
SET @IdList = ''

SELECT	
	@IdList = @IdList + CONVERT(varchar, Vendor_Distribution_Poles_PoleId) + ','
FROM	
	tb_Vendor_Distribution_Poles assets
WHERE	
	dbo.fn_DistanceBetween(assets.Latitude, assets.Longitude, @latitude, @longitude) < 2000
	AND assets.ProjectFK = @key
	AND DistributionType = 0

SELECT @IdList

  